﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing;

namespace WorkflowDesigner
{
    public class Line
    {
        private Pen m_Pen;
        /// <summary>
        /// 画笔
        /// </summary>
        public Pen Pen
        {
            get { return m_Pen; }
        }

        private bool _IsSelected = false;
        /// <summary>
        /// 是否选中状态
        /// </summary>
        public bool IsSelected
        {
            get { return this._IsSelected; }
            set
            {
                if (_IsSelected != value)
                {
                    _IsSelected = value;
                }
            }
        }

        public FlowNode StartNode { get; set; }
        public FlowNode EndNode { get; set; }

        public Line()
        {
            m_Pen = (Pen)Pens.Gray.Clone();
        }

        public void Draw(Graphics g)
        {
            if (StartNode != null && EndNode != null)
            {
                g.SmoothingMode = SmoothingMode.AntiAlias;
                AdjustableArrowCap lineArrow = new AdjustableArrowCap(4, 4, true);


                Point StartCenterPoint = new Point(StartNode.Left + StartNode.Width / 2, StartNode.Top + StartNode.Height / 2);
                Point EndCenterPoint = new Point(EndNode.Left + EndNode.Width / 2, EndNode.Top + EndNode.Height / 2);

                Point startDrawPoint, drawPoint1, endDrawPoint;

                bool IsDrewPoint1 = false;

                //开始在结束的左边
                if (StartCenterPoint.X < EndCenterPoint.X)
                {
                    #region 开始在结束的左边
                    //开始在结束的上边
                    if (StartCenterPoint.Y < EndCenterPoint.Y)
                    {
                        IsDrewPoint1 = true;
                        startDrawPoint = new Point(StartNode.Left + StartNode.Width / 2, StartNode.Top + StartNode.Height);
                        drawPoint1 = new Point(StartNode.Left + StartNode.Width / 2, EndNode.Top + EndNode.Height / 2);
                        endDrawPoint = new Point(EndNode.Left, EndNode.Top + StartNode.Height / 2);
                    }
                    else if (StartCenterPoint.Y > EndCenterPoint.Y)
                    {
                        IsDrewPoint1 = true;
                        startDrawPoint = new Point(StartNode.Left + StartNode.Width, StartNode.Top + StartNode.Height / 2);
                        drawPoint1 = new Point(EndNode.Left + EndNode.Width / 2, StartNode.Top + StartNode.Height / 2);
                        endDrawPoint = new Point(EndNode.Left, EndNode.Top + EndNode.Height / 2);
                    }
                    else
                    {
                        IsDrewPoint1 = false; 
                        drawPoint1 = new Point();
                        startDrawPoint = new Point(StartNode.Left + StartNode.Width, StartNode.Top + StartNode.Height / 2);
                        endDrawPoint = new Point(EndNode.Left, EndNode.Top + EndNode.Height / 2);
                    }
                    #endregion
                }
                else
                {
                    IsDrewPoint1 = false;
                    startDrawPoint = new Point(StartNode.Left + StartNode.Width, StartNode.Top + StartNode.Height / 2);
                    drawPoint1 = new Point();
                    endDrawPoint = new Point(EndNode.Left, EndNode.Top + EndNode.Height / 2);
                }

                if (IsDrewPoint1)
                {
                    Pen _p = (Pen)Pens.Gray.Clone();
                    g.DrawLine(_p, startDrawPoint, drawPoint1);

                    m_Pen.CustomEndCap = lineArrow;
                    g.DrawLine(m_Pen, drawPoint1, endDrawPoint);
                }
                else
                {
                    m_Pen.CustomEndCap = lineArrow;
                    g.DrawLine(m_Pen, startDrawPoint, endDrawPoint);
                }
                g.Flush();
            }
        }
    }
}
